package com.thh.shirodemo.controller;

import com.thh.shirodemo.bean.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;



/**
 * @author shkstart
 * @create 2022-07-17-23:54
 */
@RequestMapping("/common")
@Controller
public class LoginController {
    private Logger log= LoggerFactory.getLogger(LoginController.class);

    /**
     * 返回用户页面
     * @return
     */
    @RequestMapping("/goIndex")
    public String goIndex(Model model){
        return "SeckillActivity";
    }

    /**
     * 登录 认证
     * @param user
     * @param model
     * @return
     */
    @PostMapping(value = "/login")
    public String login(User user, Model model){
        //返回的信息
        model.addAttribute("msg","提示信息");
        //获取当前用户
        Subject currentUser = SecurityUtils.getSubject();
        //判断是否完成认证
//        if(!currentUser.isAuthenticated()) {
            //没认证
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getUserPass());
            try {
                //去realm中进行认证
                currentUser.login(token);
                //在session中保存登陆的用户
                currentUser.getSession().setAttribute("currentUser", currentUser.getPrincipal());
                return "forward:/doSeckill";
            } catch (UnknownAccountException uae) {
                //用户不存在
                model.addAttribute("msg","用户不存在");
                log.info("There is no user with username of " + token.getPrincipal());
                //异常都返回登录页
                return "index";
            } catch (IncorrectCredentialsException ice) {
                //密码不正确
                model.addAttribute("msg","密码不正确");
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
                //异常都返回登录页
                return "index";
            } catch (LockedAccountException lae) {
                //账户已锁定
                model.addAttribute("msg","账户被锁定");
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
                //异常都返回登录页
                return "index";
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                //用户登录失败
                model.addAttribute("msg","用户登录失败");
                //异常都返回登录页
                return "index";
                //unexpected condition?  error?
            }
//        }else {
//            msg.put("已完成认证","用户为:"+currentUser.getSession().getAttribute("currentUser"));
//            //完成认证就去秒杀
//            return "redirect:/doSeckill";
//        }

    }

    /**
     * 获取当前用户
     */
    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(){
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return session.getAttribute("currentUser");
    }

//    @RequestMapping("/logout")
//    public void logout(){
//        Subject subject = SecurityUtils.getSubject();
//        //登出
//        subject.logout();
//    }

    @RequestMapping("/unauthorized")
    public Object unauthorized(){
        return "未经授权，无法访问";
    }
}
